function Ratio = sharpe(Asset, Cash)
%SHARPE Compute the Sharpe ratio for one or more assets.
%	Given NUMSERIES assets with NUMSAMPLES returns for each asset in a
%	NUMSAMPLES x NUMSERIES matrix Asset and given either a scalar Cash asset
%	return or a vector of Cash asset returns, compute the Sharpe ratio for
%	each asset.
%
%	sharpe(Asset);
%	sharpe(Asset, Cash);
%	Ratio = sharpe(Asset, Cash);
%
% Inputs:
%	Asset - NUMSAMPLES x NUMSERIES matrix with NUMSAMPLES observations of
%		asset returns for NUMSERIES asset return series.
%
% Optional Inputs:
%	Cash - Either a scalar return for a riskless asset or a vector of asset
%		returns to be a proxy for a riskless asset. In either case, the
%		periodicity must be the same as the periodicity of Asset, e.g., if Asset
%		is monthly data, then Cash must be monthly returns. If no value is
%		supplied, the default value for Cash returns is 0.
%
% Outputs:
%	Ratio - A 1 x NUMSERIES row vector of Sharpe ratios for each series in
%		Asset. Any series in Asset with standard deviation of returns equal to 0
%		will have a NaN value for its Sharpe ratio.
%
% Notes:
%	If Cash is a vector, Asset and Cash need not have the same number of returns
%	but must have the same periodicity of returns. Note that the classic Sharpe
%	ratio assumes that Cash is riskless. In reality, a short-term cash rate is
%	not necessarily riskless.
%
%	NaN values in the data are ignored.
%
% References:
%	[1] William F. Sharpe, "Mutual Fund Performance," Journal of Business, Vol.
%		39, No. 1, Part 2, January 1966, pp. 119.138.
%
%	See also: inforatio, portalpha

%	Copyright 1995-2006 The MathWorks, Inc.
%	$Revision: 1.1.6.2 $   $Date: 2006/06/16 20:10:28 $

% Step 1 - check arguments

if nargin < 1 || isempty(Asset)
	error('Finance:sharpe:MissingInputArg', ...
		'Missing required input argument Asset.');
end

if ~isscalar(Asset) && isvector(Asset) && isa(Asset,'double')
	Asset = Asset(:);
	n = 1;
elseif ndims(Asset) == 2 && min(size(Asset)) > 1 && isa(Asset,'double')
	n = size(Asset,2);
else
	error('Finance:sharpe:InvalidInputArg', ...
 		'Invalid format for Asset returns. Must be a vector or matrix.');
end

if nargin < 2 || isempty(Cash)
	warning('Finance:sharpe:DefaultInputArg', ...
		'No Cash return specified. Will assume return is 0.');
	C0 = 0;
else
	if isscalar(Cash) && isa(Cash,'double')
		C0 = Cash;
	elseif isvector(Cash) && isa(Cash,'double')
		C0 = nanmean(Cash);
	else
		error('Finance:sharpe:InvalidInputArg', ...
			'Invalid format for Cash returns. Must be a scalar or vector.');
	end
end

% Step 2 - Compute Sharpe ratio

Denom = nanstd(Asset, 1);
zDenom = max(Asset) == min(Asset);
Ratio(zDenom) = NaN;
Ratio(~zDenom) = 1 ./ Denom(~zDenom);
Ratio = Ratio .* (nanmean(Asset) - C0 .* ones(1,n));
